<?php
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>汽车之家论坛</title>
    <link href="css/slide.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="singup/css/style.css"/>
    <link rel="stylesheet" type="text/css" href="css/easydialog.css">
    <link rel="stylesheet" type="text/css" href="css/easyui.css">
    <link rel="stylesheet" type="text/css" href="css/icon.css">
    <link rel="stylesheet" type="text/css" href="css/main.css">
    <link rel="stylesheet" type="text/css" href="css/layui.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/jquery.form.min.js"></script>
    <script src="js/jquery.easyui.min.js"></script>
    <script src="js/responsiveslides.min.js"></script>
    <script src="js/easydialog.min.js"></script>
    <script src="js/easyui-lang-zh_CN.js"></script>
    <script src="js/site.js"></script>
    <script src="layer/layer/layer.js"></script>
    <script type="text/javascript" charset="utf-8" src="ueditor/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="ueditor/ueditor.all.min.js"></script>
    <!--建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败-->
    <!--这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文-->
    <script type="text/javascript" charset="utf-8" src="ueditor/lang/zh-cn/zh-cn.js"></script>
    <style>
        .post{position: fixed;right: 10px;bottom: 30px;z-index: 9999;}
        .l-btn-text{font-size: 24px;text-align: center;color: red;margin: 0}
    </style>
    <style>
        .subTitle { width: 70%; float: left; color: black; font-size: 18px; font-weight: bold; margin: 8px 0 8px 20px; }

        .hits { width: 25%; float: right; color: gray; line-height: 35px;}

        .subContents { border-top: 1px solid #cccccc; padding: 8px; }

        .subInfo { width: 20%; min-width: 130px; float: left; text-align: center; }

        .topicContents { width: 79%; float: left; border-left: solid 1px #cccccc; min-height:260px;}

        .subInfoPic { width: 80%; min-width: 120px; }

        .subInfo ul li:first-child { color: #1c8ee5; font-weight: bold; font-size: 14px; }

        .subInfo ul li { line-height: 25px; }

        .topicContentsTitle { border-bottom: 1px solid #ccc; margin-bottom: 18px; font-size: 16px; font-weight: bold; padding: 8px; }

        .topicContentsC { padding: 8px; line-height: 25px;min-height: 150px; }
        .sign{margin: 10px;border-top:1px dashed #cccccc; padding-top: 5px;}
    </style>
</head>
<body>
<div class="top">
    <div class="top_main">
        <div class="logo" onclick="javascript:location.href='index.php';">我的汽车之家</div>

        <?php
        if(isset($_SESSION['username']) and $_SESSION['username'] !=""){
            ?>
            <div id="login" data-name="注销">注销</div>
            <?php
        }
        else{
            ?>
            <div id="login" data-name="登录">登录</div>
            <?php
        }
        ?>
        <div class="search">
            <form action="" method="post"><input name="search"><input type="submit" value="论坛搜索" class="btnSearch">
            </form>
        </div>
        <div class="clear"></div>
    </div>
    <?php
    include_once 'top.php'
    ?>
    <?php
    include_once 'info.php';
    include_once 'conn.php';
    $sql = "SELECT m.*,case when s.replyNumbers is null then 0 else s.replyNumbers end replyNumbers from (SELECT m.*,u.nickname,u.postDate,u.pic,u.grade from maintopic m ,userinfo u where m.userid = u.id and m.id = ".$_GET['id'].") m
left JOIN (SELECT count(1) replyNumbers,maintopicid from subtopic GROUP BY maintopicid) s on m.id = s.maintopicid";
    $result = mysqli_query($conn,$sql);
    $info = mysqli_fetch_array($result);
    ?>
    <div class="mainContent">
        <div class="subTitle"><?php echo $info['title'];?> </div>
        <div class="hits">点击次数：<?php echo $info['hits'];?> &nbsp;&nbsp;&nbsp;&nbsp; 回复次数：<?php echo $info['replyNumbers']?></div>
        <div class="clear"></div>
        <div class="subContents">
            <div class="subInfo">
                <ul>
                    <li><?php echo $info['nickname'];?></li>
                    <li><img class="subInfoPic" src="tx/<?php echo $info['pic'];?>"</li>
                    <li>注册日期：<?php echo $info['postDate'];?></li>
                    <li>等级：<?php echo $info['grade'];?></li>
                    <li></li>
                </ul>
            </div>
            <!--重新查询当前主贴的所有回复内容-->
            <div class="topicContents">
                <div class="topicContentsTitle"> <?php echo $info['title'];?></div>
                <div class="topicContentsC"><?php echo $info['contents'];?></div>
                <div class="sign">用户IP:&nbsp;<?php echo $info['ip'];?>&nbsp;
                    <?php
                    if(isset($_SESSION['isAdmin']) and $_SESSION['isAdmin'] == 1){
                    ?>
                    <a href='delTopic.php?id=<?php echo $info['id']?>'>删除本贴</a>
                <?php }?></div>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </div>
        <?php
        include_once 'conn.php';
        $sql = "select s.*,u.nickname,u.postDate,u.grade,u.pic
from subtopic s,userinfo u where u.id=s.userid and maintopicid = " . $info['id'];
        $result = mysqli_query($conn,$sql);
        while($info = mysqli_fetch_array($result)){
        ?>
        <div class="subContents">
            <div class="subInfo">
                <ul>

                    <li><?php echo $info['nickname']?></li>
                    <li><img class="subInfoPic" src="tx/<?php echo $info['pic']?>"</li>
                    <li>注册日期：<?php echo $info['postDate']?></li>
                    <li>等级：<?php echo $info['grade']?></li>
                    <li></li>
                </ul>
            </div>
            <div class="topicContents">
                <div class="topicContentsC">
                    <?php
                    if($info['quoteid']){
                        $sql = "select contents from subtopic where id = " .$info['quoteid'];
                        $result_quote = mysqli_query($conn,$sql);
                        $info_quote = mysqli_fetch_array($result_quote);
                    ?>
                    <fieldset>
                        <legend>引用回复</legend>
                        <?php
                        echo $info_quote['contents'];
                        unset($result_quote);
                        unset($info_quote);?>
                    </fieldset>
                    <?php
                    }
                    ?>
                    <?php echo $info['contents']?>
                </div>
                <div class="sign">
                    <b>用户IP：</b><?php echo $info['ip']?>&nbsp;&nbsp; <a href='delTopic.php?id=<?php echo $info["id"]?>'>删除本贴</a>&nbsp;&nbsp;&nbsp;<span onclick='quoteReply(<?php echo $info["id"]?>)'><a href='#editor'>回复TA</a></span>
                </div>
                <div class="clear"></div>
            </div>
            <div class="clear"></div>
        </div>
        <?php }?>
        <script id="editor" type="text/plain" style="width:100%;height:200px;" ></script>
    </div>
    <div class="clear"></div>

    <div style="width: 80%;margin: 10px auto">
        <div class="count">
            <span class="newPost" onclick="newReply(1);">引用回贴</span>
            <span class="newPost" onclick="newReply(0);" style="margin-right:10px;">直接回贴</span>
            <span class="newPost" onclick="location.href='index.php'">返回首页</span>
        </div>
    </div>
    <div class="bot">版权所有 汽车之家</div>
    <!--<div class="post"><a id="btn" href="#" class="easyui-linkbutton" data-options="{iconCls:'icon-search',width:'150px',height:'50px'}">发新贴</a></div>-->
    <div id="registerForm" class="form_wrapper" style="width: 550px;">
        <!--<form class="register">-->
        <h3>用户注册</h3>
        <div class="column">
            <div>
                <label>用户名:</label>
                <input type="text" name="username" id="username">
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>昵称:</label>
                <input type="text" name="nickname" id="nickname">
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>出生日期:</label>
                <div class="birthday">
                    <input type="text"  id="birthday" class="datebox-f combo-f textbox-f" textboxname="birthday" style="display: none;" comboname="birthday">
                </div>
            </div>
        </div>
        <div class="column">
            <div>
                <label>选择头像:</label>
                <form id="uploadPic" action="uploadPic.php" method="post" ecntype="mulitpart/form-data">
                    <input type="file" name="pic" id="pic">
                </form>
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>电子信箱:</label>
                <input type="text" name="email" id="email">
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>密码:</label>
                <input type="password" name="pw" id="pw">
                <span class="error">This is an error</span>
            </div>
        </div>
        <div class="bottom">
            <div class="remember"></div>
            <input type="button" value="注册" id="singup">
            <a href="javascript:void(0);" rel="login" class="linkform" id="showLogin">已经有帐号了? 点击登录</a>
            <div class="clear"></div>
        </div>
        <div class="clear"></div>
        <!--</form>-->
        <div class="clear"></div>
    </div>
    <div class="clear"></div>
    <div id="loginFrom" class="form_wrapper">
        <form class="login">
            <h3>用户登录</h3>
            <div>
                <label>用户名:</label>
                <input type="text" id="username2">
                <span class="error">This is an error</span>
            </div>
            <div>
                <label>密码: <a href="singup/forgot_password.html" rel="forgot_password"
                              class="forgot linkform">忘记密码?</a></label>
                <input type="password" id="pw2">
                <span class="error">This is an error</span>
            </div>
            <div class="bottom">
                <div class="remember"><input type="checkbox" value="1"  name="rememberMe"><span>保存登录状态</span></div>
                <input type="button" value="登录" onclick="login();">
                <a href="javascript:void(0);" rel="register" class="linkform" id="showRegister">还没有帐号? 点击注册</a>
                <div class="clear"></div>
            </div>
        </form>
    </div>
    <div class="clear"></div>

    <script>
        var ue = UE.getEditor('editor');
        var quoteID = 0;//引用ID初始值，0表示无。
        var mainID = <?php echo $_GET['id'];?>;
        function login(){
            if($("#username2").val().trim() == "" || $("#pw2").val().trim() == ""){
                alert("用户名和密码必须填写！");
                return;
            }
            $.ajax({
                url:'postLogin.php',
                data:{
                    username:$("#username2").val().trim(),
                    pw:$("#pw2").val().trim(),
                    action:'0'//action:0表示手动登录
                },
                type:'post',
                dataType:'json',
                success:function (d) {
                    if(d.error == 1){
                        alert(d.errMsg);
                    }else{
                        //登录成功，判断用户是否有勾选“保存登录”选项。如果有，则使用localstorage保存登录信息
                        if($('input[name="rememberMe"]:checked').val() == 1){
                            localStorage.setItem('rememberMe',1);
                            localStorage.setItem('username',$("#username2").val().trim());
                        }
                        else{
                            localStorage.setItem('rememberMe','');
                            localStorage.setItem('username','');
                        }
                        easyDialog.close();
                        //更新info里面的各项内容
                        getInfo();
                        $(".info").show();
                    }
                },
                error:function () {
                    alert('网络错误');
                }
            })
        }
        function getInfo(){
            $.ajax({
                url:'getInfo.php',
                dataType:'json',
                success:function (d) {
                    if(d.error == 1){
                        alert(d.errMsg);
                    }
                    else{
                        $("#tx1").attr("src","tx/"+d.info['pic']);
                        $("#username1").text(d.info['username']);
                        $("#nickname1").text(d.info['nickname']);
                        $("#singUpDate").text(d.info['postDate']);
                        $("#total").text(d.info['nb']);
                        $("#lastPostTime").text(d.info['lp']);
                        $("#lastTitle").text(d.info['title']);
                        $("#lastReplyTime").text(d.info['lastreplytime']);
                        //登录成功，在右上角显示“注销”
                        $("#login").attr('data-name','注销').text('注销');
                    }
                },
                error:function () {

                }
            })
        }
        function total() {
            $.ajax({
                url:'getTotal.php',
                dataType:'json',
                success:function (d) {
                    $("#total1").text(d.numbers);
                }
            })
        }
        function close1(){
            layer.closeAll();
            //window.location.reload();
        }
        function quoteReply(id) {
            //alert(111111111111)
            quoteID = id;
        }
        function newReply(action){
            if(action == 1){
                //表示引用回复，需要判断当前是否有引用id
                if(quoteID == 0){
                    //说明没有点引用回复，不能引用回复
                    alert('引用回复前，必须点击“回复TA”以确定引用哪一个回复');
                    return;
                }
                //开始回复
                reply(quoteID);
            }
            else{
                //直接回复
                quoteID = 0;
                reply(0);
            }
        }
        function reply(replyID){
            var contents = ue.hasContents();
            if(!contents){
                alert('回复内容必须填写！');
                return;
            }
            $.ajax({
                url:'postSubTopic.php',
                data:{contents:ue.getContent(),replyID:replyID,mainID:mainID},
                type:'post',
                dataType:'json',
                success:function (d) {
                    if(d.error == 1){
                        alert(d.errMsg);
                    }
                    else{
                        alert('回贴成功！');
                        location.reload();
                    }
                },
                error:function () {
                    alert('网络错误');
                }
            })
        }
        $(function () {
            //先判断当前是否登录。如果登录了，则显示info区域
            <?php
            if(isset($_SESSION['username']) and $_SESSION['username'] != ""){
            //说明已经处于登录状态
            ?>
            getInfo();
            $(".info").show();
            <?php
            }
            else{
                //说明未登录，则判断是否有自动登录
                echo "autoLogin();";
            }
            ?>

            //再判断当前是否保存了上一次的登录状态，如果有登录状态，则需要模拟登录
            function autoLogin(){
                if(localStorage.getItem('rememberMe') == 1){
                    $.ajax(
                        {
                            url:'postLogin.php',
                            data:{
                                username:localStorage.getItem('username'),
                                action:'1'},//action:1表示自动登录
                            type:'post',
                            dataType:'json',
                            success:function (d) {
                                if(d.error == 0){
                                    getInfo();
                                    $(".info").show();
                                }
                                else{
                                    localStorage.setItem('rememberMe',0);
                                    localStorage.setItem('username','');
                                }
                            }
                        }
                    )
                }
            }
            $("#singup").click(function () {
                let errLog = 0, errMsg = '';
                if($("#username").val().trim() == ""){
                    errLog = 1;
                    errMsg += "用户名必须填写！\n";
                }
                if($("#pw").val().trim() == ""){
                    errLog = 1;
                    errMsg += "密码必须填写！\n";
                }
                if($("#nickname").val().trim() == ""){
                    errLog = 1;
                    errMsg += "昵称必须填写！\n";
                }
                if($("#birthday").datebox('getValue') == ""){
                    errLog = 1;
                    errMsg += "生日必须选择！\n";
                }
                if($("#pic").val().trim() == ""){
                    errLog = 1;
                    errMsg += "必须上传头像！\n";
                }
                if(errLog == 1){
                    alert(errMsg);
                }
                else{
                    //各项数据填写完整，可以开始注册
                    singup();
                }
            })
            function singup(){
                $("#uploadPic").ajaxSubmit({
                    /*url:"uploadPic.php",
                    type:'post',*/
                    dataType:"json",
                    success:function (d) {
                        if(d.error == 1){
                            alert(d.errMsg);
                        }
                        else{
                            //说明头像上传OK，可以注册，写入数据库
                            $.ajax({
                                url:'postSingup.php',
                                data:{
                                    username:$("#username").val().trim(),
                                    pw:$("#pw").val().trim(),
                                    nickname:$("#nickname").val().trim(),
                                    birthday:$("#birthday").datebox('getValue'),
                                    email:$("#email").val().trim(),
                                    filename:d.fileName
                                },
                                dataType: 'json',
                                type:'post',
                                success:function (d) {
                                    if(d.error ==1){
                                        alert(d.errMsg);
                                    }
                                    else{
                                        alert('注册成功');
                                        easyDialog.close();
                                        $("#login").trigger('click');
                                    }
                                },
                                error:function () {

                                }
                            })
                        }
                        $("#uploadPic").resetForm();
                    },
                    error:function () {
                        alert(11111);
                    }
                })
            }
            $("#birthday").datebox({
                required: true,
                panelWidth: 200,
                panelHeight: 250
            })
            $("#_easyui_textbox_input1").focus(function () {
                $("#_easyui_textbox_input1").blur();
            })
            $("#slider").responsiveSlides({
                speed: 1000
            });

            $("#login").click(function () {
                if($(this).attr('data-name') == '注销'){
                    location.href='logout.php';
                }
                else{
                    easyDialog.open({
                        container: 'loginFrom',
                        fixed: false
                    })
                }

            })
            $("#showRegister").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container: 'registerForm',
                    fixed: false
                })
            })
            $("#showLogin").click(function () {
                easyDialog.close();
                easyDialog.open({
                    container: 'loginFrom',
                    fixed: false
                })
            })

            total();
            $("#btn").click(function () {
                layer.open({
                    type: 2,
                    title:'发新贴',
                    area: ['900px', '640px'],
                    fixed: false, //不固定
                    maxmin: true,
                    content: 'ueditor/index.html'
                });
            })


        })

    </script>

</div>


</body>
</html>